import json

from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts

# 读取数据文件
file = open("E:/storage/study/Python/python-learn/day01/02-python-learn/10_可视化案例/素材/疫情.txt", "r", encoding="UTF-8")
# 读取全部数据
data = file.read()
# 关闭文件
file.close()

# 取到各省数据
# 将字符串json转换为python的字典
data_dic = json.loads(data) # 基础数据字典
# 从字典中取出省份的数据
province_data_list = data_dic['areaTree'][0]['children']

# 组装每个省份和确诊人数为元组，并各个省的数据都封装到列表内
data_list = [] # 绘图需要用的数据列表
for province_data in province_data_list:
    province_name = province_data['name'] # 省份名称
    if ((province_name=="北京") | (province_name=="上海") | (province_name == "天津") | (province_name == "重庆")):
        province_name += "市"
    elif (province_name=="内蒙古") | (province_name=="西藏"):
        province_name += "自治区"
    elif (province_name=="广西"):
        province_name += "壮族自治区"
    elif (province_name=="新疆"):
        province_name += "维吾尔自治区"
    elif (province_name=="宁夏"):
        province_name += "回族自治区"
    elif (province_name=="香港") | (province_name=="澳门"):
        province_name += "特别行政区"
    else:
        province_name += "省"

    confirm = province_data['total']['confirm'] # 确诊人数
    data_list.append((province_name,confirm))

# 创建地图对象
map = Map()
# 添加数据
map.add("各省份确诊人数", data_list, 'china')

# 设置全局配置，定制分段的视觉映射
map.set_global_opts(
    visualmap_opts = VisualMapOpts(
        is_show=True, # 是否显示
        is_piecewise=True, # 是否分段
        pieces=[
            {"min": 0, "max": 99, "label": "0-99人", "color": 'green'},
            {"min": 100, "max": 999, "label": "100-999人", "color": 'black'},
            {"min": 1000, "max": 5000, "label": "1000-5000人", "color": 'yellow'},
            {"min": 5000, "max": 9999, "label": "5000-9999人", "color": 'blue'},
            {"min": 10000, "max": 99999, "label": "10000-99999人", "color": 'red'},
            {"min": 100000, "label": "100000人以上", "color": 'pink'}
        ]
    )
)

# 绘图
map.render("全国疫情地图.html")